package cn.iocoder.yudao.module.security.controller.admin.whitelistoperate;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.whitelistoperate.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.whitelistoperate.WhitelistOperateDO;
import cn.iocoder.yudao.module.security.service.whitelistoperate.WhitelistOperateService;

@Tag(name = "管理后台 - 白名单操作")
@RestController
@RequestMapping("/security/whitelist-operate")
@Validated
public class WhitelistOperateController {

    @Resource
    private WhitelistOperateService whitelistOperateService;

    @PostMapping("/create")
    @Operation(summary = "创建白名单操作")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:create')")
    public CommonResult<Long> createWhitelistOperate(@Valid @RequestBody WhitelistOperateSaveReqVO createReqVO) {
        return success(whitelistOperateService.createWhitelistOperate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新白名单操作")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:update')")
    public CommonResult<Boolean> updateWhitelistOperate(@Valid @RequestBody WhitelistOperateSaveReqVO updateReqVO) {
        whitelistOperateService.updateWhitelistOperate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除白名单操作")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:delete')")
    public CommonResult<Boolean> deleteWhitelistOperate(@RequestParam("id") Long id) {
        whitelistOperateService.deleteWhitelistOperate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得白名单操作")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:query')")
    public CommonResult<WhitelistOperateRespVO> getWhitelistOperate(@RequestParam("id") Long id) {
        WhitelistOperateDO whitelistOperate = whitelistOperateService.getWhitelistOperate(id);
        return success(BeanUtils.toBean(whitelistOperate, WhitelistOperateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得白名单操作分页")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:query')")
    public CommonResult<PageResult<WhitelistOperateRespVO>> getWhitelistOperatePage(@Valid WhitelistOperatePageReqVO pageReqVO) {
        PageResult<WhitelistOperateDO> pageResult = whitelistOperateService.getWhitelistOperatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WhitelistOperateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出白名单操作 Excel")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:export')")
    @OperateLog(type = EXPORT)
    public void exportWhitelistOperateExcel(@Valid WhitelistOperatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WhitelistOperateDO> list = whitelistOperateService.getWhitelistOperatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "白名单操作.xls", "数据", WhitelistOperateRespVO.class,
                        BeanUtils.toBean(list, WhitelistOperateRespVO.class));
    }

    @PostMapping("/start")
    @Operation(summary = "创建白名单操作")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:create')")
    public CommonResult<Long> startWhitelistOperate() {
        return success(whitelistOperateService.startWhitelistOperate());
    }

    @PostMapping("/stop")
    @Operation(summary = "创建白名单操作")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:create')")
    public CommonResult<Long> stopWhitelistOperate() {
        return success(whitelistOperateService.stopWhitelistOperate());
    }

    @GetMapping("/detail")
    @Operation(summary = "获得白名单操作")
    //@Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:whitelist-operate:query')")
    public CommonResult<WhitelistOperateRespVO> getLatestWhitelistOperate() {
        WhitelistOperateDO whitelistOperate = whitelistOperateService.getLatestWhitelistOperate();
        WhitelistOperateRespVO vo = BeanUtils.toBean(whitelistOperate, WhitelistOperateRespVO.class);
        if (Objects.nonNull(vo)) {
            vo.setLearning(LocalDateTimeUtils.afterNow(whitelistOperate.getEndTime()));
        }
        return success(vo);
    }

}